package com.slipper.system.codeless.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import com.google.gson.reflect.TypeToken;
import com.slipper.core.cache.CacheKey;
import com.slipper.core.cache.CacheUtil;
import com.slipper.system.codeless.bo.PageCompFilterBO;
import com.slipper.system.codeless.dao.PageCompFilterDao;
import com.slipper.core.transaction.ReadTransactional;
import com.slipper.core.transaction.WriteTransactional;
import com.slipper.util.Tool;
import com.slipper.util.UUID;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;

import java.util.List;

@Service
@ReadTransactional
public class PageCompFilterService {
    @Resource
    private PageCompFilterDao pageCompFilterDao;
    
    public PageCompFilterBO queryBy(String id) {
        return pageCompFilterDao.queryBy(id);
    }
    
    public List<PageCompFilterBO> queryList(PageCompFilterBO bo) {
        return pageCompFilterDao.queryList(bo);
    }
    
    public PageInfo<PageCompFilterBO> queryPage(PageCompFilterBO bo) {
        PageHelper.startPage(bo.getPageNumber(), bo.getPageSize());
        return new PageInfo<>(pageCompFilterDao.queryList(bo));
    }

    @WriteTransactional
    public int save(String compId, List<PageCompFilterBO> list, String employeeId) {
        if (Tool.isBlank(compId)) {
            return 0;
        }

        for (int i=0;i<list.size(); i++) {
            PageCompFilterBO button = list.get(i);
            button.setCompId(compId);
            button.setOrdinal(i + 1);
            if (Tool.isBlank(button.getId())) {
                button.setId(UUID.getUUID());
                button.setUpdateId(employeeId);
                button.setCreateId(employeeId);
                button.setUpdateTime(System.currentTimeMillis());
                button.setCreateTime(System.currentTimeMillis());
                pageCompFilterDao.insert(button);
            } else {
                button.setUpdateId(employeeId);
                button.setUpdateTime(System.currentTimeMillis());
                pageCompFilterDao.update(button);
            }
        }

        // 删除被删除的数据
        PageCompFilterBO qo = new PageCompFilterBO();
        qo.setCompId(compId);
        pageCompFilterDao.queryList(qo).stream()
                .filter(button -> list.stream().noneMatch(temp -> button.getId().equals(temp.getId())))
                .forEach(this::delete);
        CacheUtil.remove(CacheKey.PageCompFilter.PREFIX + compId);
        return 1;
    }

    @WriteTransactional
    public void saveList(List<PageCompFilterBO> list) {
        if (list == null) return;

        list.forEach(item -> {
            if (pageCompFilterDao.queryBy(item.getId()) != null) {
                item.setUpdateTime(System.currentTimeMillis());
                pageCompFilterDao.update(item);
            } else {
                item.setUpdateTime(System.currentTimeMillis());
                item.setCreateTime(System.currentTimeMillis());
                pageCompFilterDao.insert(item);
            }
            CacheUtil.remove(CacheKey.PageCompFilter.PREFIX + item.getCompId());
        });
    }

    @WriteTransactional
    public int delete(PageCompFilterBO bo) {
        int result = pageCompFilterDao.delete(bo.getId());
        CacheUtil.remove(CacheKey.PageCompFilter.PREFIX + bo.getCompId());
        return result;
    }

    @WriteTransactional
    public void deleteByCompId(String compId) {
        if (Tool.isBlank(compId)) {
            return;
        }

        PageCompFilterBO qo = new PageCompFilterBO();
        qo.setCompId(compId);
        pageCompFilterDao.queryList(qo).forEach(this::delete);
        CacheUtil.remove(CacheKey.PageCompFilter.PREFIX + compId);
    }

    public List<PageCompFilterBO> queryByCompId(String compId) {
        List<PageCompFilterBO> filterList = CacheUtil.get(
                CacheKey.PageCompFilter.PREFIX + compId,
                new TypeToken<List<PageCompFilterBO>>(){}.getType());
        if (filterList == null) {
            PageCompFilterBO qo = new PageCompFilterBO();
            qo.setCompId(compId);
            filterList = pageCompFilterDao.queryList(qo);
            CacheUtil.set(CacheKey.PageCompFilter.PREFIX + compId, filterList, 60 * 60);
        }
        return filterList;
    }

    @WriteTransactional
    public void copyByCompId(String oldCompId, String newCompId, String employeeId) {
        queryByCompId(oldCompId).forEach(filter -> {
            filter.setId(UUID.getUUID());
            filter.setCompId(newCompId);
            filter.setUpdateId(employeeId);
            filter.setCreateId(employeeId);
            filter.setUpdateTime(System.currentTimeMillis());
            filter.setCreateTime(System.currentTimeMillis());
            pageCompFilterDao.insert(filter);
        });
    }
}
